package com.syh.jn.scm.domain.upstream.entity.base.logistics;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.util.validator.ValidationUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.domain.upstream.repository.finance.UpStatementAccountBillReadRepository;
import com.syh.jn.scm.domain.upstream.repository.sale.UpSaleOrderReadRepository;
import com.syh.jn.scm.domain.upstream.repository.sale.UpSaleReturnOrderReadRepository;
import com.syh.jn.scm.dto.upstream.base.logistics.UpLogisticsMto;
import com.syh.jn.scm.dto.upstream.base.logistics.UpLogisticsSto;
import com.syh.jn.scm.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;

/**
 * 物流
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class UpLogistics extends AbstractSimpleAggregate<Long> {
  public UpLogistics(){}

  /**
   * 构造方法增加物流
   * @param sto
   */
  public UpLogistics(UpLogisticsSto sto){
    ExtAssert.notNull(sto, "对象为空");
    ValidationUtils.validate(sto);

    this.createTimeStamp = System.currentTimeMillis();
    this.status = Short.valueOf("1");
    ExtBeanUtils.copyProperties(sto, this);
  }
  /**
   * 修改经营主体
   * @param mto
   */
  public void modify(UpLogisticsMto mto) {
    ExtAssert.notNull(mto, "对象为空");
    ValidationUtils.validate(mto);
    ExtBeanUtils.copyProperties(mto, this);
    this.lastUpdateTimeStamp = System.currentTimeMillis();
  }
  /**
   * 数据销毁
   */
  public void destroy() {
    //查看销售单是否有数据存在
    UpSaleOrderReadRepository saleOrder = (UpSaleOrderReadRepository) SpringContextUtils.getBean(UpSaleOrderReadRepository.class);
    Wrapper saleWrapper = Wrapper.build();
    saleWrapper.append(Restrictions.eq("logisticsCompanyId", this.id));
    Integer saleCount = saleOrder.getCount(saleWrapper);
    if (saleCount > 0) {
      throw new BusinessException("销售单存在该数据,删除失败");
    }
    //查看销售退货是否有数据存在
    UpSaleReturnOrderReadRepository saleReturn = (UpSaleReturnOrderReadRepository) SpringContextUtils.getBean(UpSaleReturnOrderReadRepository.class);
    Wrapper saleReturnWrapper = Wrapper.build();
    saleReturnWrapper.append(Restrictions.eq("logisticsCompanyId", this.id));
    Integer saleReturnCount = saleReturn.getCount(saleReturnWrapper);
    if (saleReturnCount > 0) {
      throw new BusinessException("销售退货单存在该数据,删除失败");
    }
    //查看对账单是否有数据存在
    UpStatementAccountBillReadRepository statementBill = (UpStatementAccountBillReadRepository) SpringContextUtils.getBean(UpStatementAccountBillReadRepository.class);
    Wrapper statementBillWrapper = Wrapper.build();
    statementBillWrapper.append(Restrictions.eq("customerId", this.id));
    statementBillWrapper.append(Restrictions.eq("customerType", Short.valueOf("2")));
    Integer statementCount = statementBill.getCount(statementBillWrapper);
    if (statementCount > 0) {
      throw new BusinessException("对账单存在该数据,删除失败");
    }
  }
  /**
   * 启用
   */
  public void enabled() {
    this.status = Short.valueOf("1");
  }

  /**
   * 停用
   */
  public void disabled() {
    this.status = Short.valueOf("-1");
  }
  /**
   * 物流公司名字
   */
  private String name;
  /**
   * 公司的联系电话
   */
  private String telephoneNum;
  /**
   * 是否提供冷链车: -1不提供,1提供
   */
  private Short isProvide;
  /**
   * 状态: -1停用, 1可用
   */
  private Short status;
  /**
   * 归属id
   */
  private Long businessEntityId;
  /**
   * 期初应收总额
   */
  private BigDecimal beginReceiveTotalAmount;
  /**
   * 期初应付总额
   */
  private BigDecimal beginPaymentTotalAmount;
}
